/**
 * File  : SettingThread.cc
 * Time  : 2015-5-19 10:11
 * Author: YangBo 
 * Ref   : 
 */
#include "WorkThreads.h"
#include "./log/Log_Def.h"
#include "./setting/I2CInterface.h"
#include "./ThreadMutex.h"
#include <iostream>
#include <fstream>
using namespace std;

#define SEND_I2C true
//TODO
void SettingThread::Run ()  {
  
  
  LED_I2C *i2c= new LED_I2C(0x70);
  LogType log_type_;
  ostringstream oss;
  LOG_INFO(LOG_LEVEL_INFO) << "SET LOG ## " << "this is set starting";
  LOG_ADD;
  cout << "set thread" << endl;
	fstream fp;
	uint32_t tmp=0;
  //set_param = new UserSettings;
  for(;;) {
	
		
//    Type<uint32_t> *tmp = event_queue_->GetEvent();
//    cout<< hex <<tmp->opcode() << endl; 
  tmp=0;

		do{
			mutex_set_.Lock();
			fp.open("./setting/setting.txt",std::fstream::in);
			fp>>tmp;
			mutex_set_.Unlock();  
		}while(!fp.is_open());
		fp.close();
	

    //sending setting information via I2C
    if(SEND_I2C == true && tmp != 0) {
      const char* dev = "/dev/i2c-2";

    int num = i2c->LED_I2C_open(dev);
      if(num == 0){
        LOG_INFO(LOG_LEVEL_ERROR) << "SET LOG $$ " << "I2C OPEN FAILED";
        LOG_ADD;
      }
      else {
        LOG_INFO(LOG_LEVEL_INFO) << "SET LOG $$ " << "I2C OPEN SUCCESS";
        LOG_ADD;
      }
      
     num = i2c->LED_I2C_writedata(0x0,tmp);
			sleep(1);
      if(num <0 ){
        LOG_INFO(LOG_LEVEL_ERROR) << "SET LOG $$ " << "I2C WRITE DATA FAILED";
        LOG_ADD;
      }
      else{
        LOG_INFO(LOG_LEVEL_INFO) << "SET LOG $$ " << "I2C WRITE DATA SUCCESS";
        LOG_ADD;
      }
			sleep(1);
      LOG_INFO(LOG_LEVEL_INFO) << "SET LOG $$ " << "SEND I2C DATA " << tmp;
      LOG_ADD;
    }
		sleep(1);
   // cout << "setting thread sucess!" << endl;
    LOG_INFO(LOG_LEVEL_INFO) << "SET LOG $$ " << "SET mission DONE!";
    LOG_ADD;

  }
     
}
